<template>
  <div id="user-edit">
    <el-form ref="user" :model="user" label-width="80px">
      <el-form-item label="登录名">
        <el-input v-model="user.userName"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="user.start" placeholder="请选择状态">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-input v-model="user.foundTime"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateBlog">更新</el-button>
        <el-button @click="goBack">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "user-edit",
    data() {
      return {
        id: this.$route.params.id,
        user: []
      }
    },
    created() {
      let _this = this
      this.$axios({
        url: "/user/" + _this.$route.params.id,
        method: "get"
      }).then(function (data) {
        console.log(data)
        _this.user = data.data.data
      }).catch(function (error) {
        console.error(error)
      })
    },
    methods: {
      updateBlog() {
        let _this = this;
        let user = {
          "id": _this.user.id,
          "userName": _this.user.userName,
          "password": _this.user.password,
          "start": _this.user.start
        };
        let postData = this.$qs.stringify(user)
        this.$axios({
          url: "/user",
          method: "put",
          data: postData
        }).then(function (data) {
          console.log(data);
          _this.open2();
          _this.goBack();
        }).catch(function (error) {
          console.error(error)
        })
      },
      goBack() {
        // console.log('go back');
        // 跳转到上一页面
        this.$router.go(-1)
      },
      open2() {
        this.$message({
          message: '更新用户信息成功',
          type: 'success'
        })
      }
    }
  }
</script>

<style scoped>
  #user-edit {
    margin: 50px 750px;
    width: 400px;
    border: aqua;
    padding: initial;
  }
</style>
